/** 
 * Copyright (C) 2012 Martin Řehánek <Martin.Rehanek at mzk.cz>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Martin Řehánek <Martin.Rehanek at mzk.cz>
 */

goog.provide('kramerius.audio');
goog.provide('kramerius.audio.Renderer');

//information about playing track
goog.require('kramerius.audio.ui.PlayingTrackInfo');
goog.require('kramerius.audio.ui.Timeline');
goog.require('kramerius.audio.ui.ProgressInfo');
//player controll components
goog.require('kramerius.audio.ui.PlayPauseButton');
goog.require('kramerius.audio.ui.StopButton');
goog.require('kramerius.audio.ui.PlayPreviousButton');
goog.require('kramerius.audio.ui.PlayNextButton');
goog.require('kramerius.audio.ui.PlaylistShowHideButton');
goog.require('kramerius.audio.ui.VolumeSlider');
goog.require('kramerius.audio.ui.MuteButton');

//controller
goog.require('kramerius.audio.Controller');
//audio player (uses soundmanager)
goog.require('kramerius.audio.AudioPlayer');
//playlist 
goog.require('kramerius.audio.ui.playlist.Playlist');
goog.require('kramerius.audio.ui.playlist.Item');
//tracks
goog.require('kramerius.audio.TrackFactory');
goog.require('kramerius.audio.AbstractTrack');
goog.require('kramerius.audio.Track');
goog.require('kramerius.audio.DebugTrack');
//rotating record
goog.require('kramerius.audio.ui.RotatingRecord');



//goog.exportSymbol('kramerius.audio.createRenderer','kramerius.audio.createRenderer');
////goog.exportSymbol(Renderer.prototype,'addNewTrack',Renderer.prototype.addNewTrack);
////goog.exportSymbol(Renderer.prototype,'appendCss',Renderer.prototype.appendCss);
////goog.exportProperty(Renderer.prototype,'addNewTrack',Renderer.prototype.addNewTrack);
////goog.exportProperty(Renderer.prototype,'appendCss',Renderer.prototype.appendCss);
//goog.exportSymbol('kramerius.audio.Renderer.prototype.appendCss','Renderer.prototype.appendCss');

//TODO: uncomment in production
if (!window.debugMode){
    window['AudioRenderer'] = kramerius.audio.Renderer;
    AudioRenderer.prototype['appendCss'] = kramerius.audio.Renderer.prototype.appendCss;
    AudioRenderer.prototype['addNewTrack'] = kramerius.audio.Renderer.prototype.addNewTrack;
}

//signals, that this script is loaded
window['audioScriptsLoaded']=true;

/** @expose */
kramerius.audio.createRenderer = function(audioProxyUrl,swfUrl, loggingEnabled){
    return new kramerius.audio.Renderer(audioProxyUrl, swfUrl, loggingEnabled);
}

/**
 * @constructor 
 * 
 * 
 */
kramerius.audio.Renderer = function(audioProxyUrl, swfUrl, loggingEnabled){
    /** @private */
    this.loggingEnabled_ = loggingEnabled;
    this.log_('initializing');
    /** @private */
    this.controller_ = new kramerius.audio.Controller(loggingEnabled);
    var debugTrackBaseUrl = 'http://localhost/gramodesky/STALIN_shellack/';
    this.trackFactory_ = new kramerius.audio.TrackFactory(audioProxyUrl, debugTrackBaseUrl);
    //playlist
    var playlistContainer = goog.dom.getElement('audio.playlist');
    /** @private */
    this.playlist_ = new kramerius.audio.ui.playlist.Playlist(playlistContainer, this.controller_, loggingEnabled);
    this.initVisualComponents_(this.controller_, loggingEnabled);
    this.initAudioPlayer_(this.controller_, swfUrl, loggingEnabled);
//console.log(this.controller_);
//console.log(this.playlist_);
}

/** @private */
kramerius.audio.Renderer.prototype.initVisualComponents_ = function(controller, loggingEnabled){
    this.log_('initializing visual components');
    
    //play/pause button
    this.log_('rendering PLAY/PAUSE button');
    new kramerius.audio.ui.PlayPauseButton(
        goog.dom.getElement('audio.playPauseBtn'), controller, loggingEnabled);
    
    //play-next button
    this.log_('rendering PLAY-NEXT button');
    new kramerius.audio.ui.PlayNextButton(
        goog.dom.getElement('audio.playNextBtn'), controller, loggingEnabled);
        
    //play-previous button
    this.log_('rendering PLAY-PREVIOUS button');    
    new kramerius.audio.ui.PlayPreviousButton(
        goog.dom.getElement('audio.playPreviousBtn'), controller, loggingEnabled);
    
    //playing track info
    this.log_('rendering track info');
    new kramerius.audio.ui.PlayingTrackInfo(
        goog.dom.getElement('audio.playingTrackInfo'), controller);
    
            
    //progress bar
    this.log_('rendering timeline');
    new kramerius.audio.ui.Timeline(
        goog.dom.getElement('audio.timeline'), controller);
            
    //progress info
    this.log_('rendering progress info');
    new kramerius.audio.ui.ProgressInfo(
        goog.dom.getElement('audio.progressInfo'), controller);
            
    //playlist hide/show
    this.log_('rendering SHOW/HIDE PLAYLIST BUTTON');
    new kramerius.audio.ui.PlaylistShowHideButton(
        goog.dom.getElement('audio.playlistButton'), controller);
            
    //volume (mute/unmute) button
    this.log_('rendering MUTE/UNMUTE button');
    new kramerius.audio.ui.MuteButton(
        goog.dom.getElement('audio.muteBtn'), controller);
            
    //volume slider
    this.log_('rendering volume slider');
    new kramerius.audio.ui.VolumeSlider(
        goog.dom.getElement('audio.volumeSlider'), controller, loggingEnabled);
    this.log_('rendering volume slider - FINISHED');    
    
    //html 5 canvas support
    if (!!document.createElement('canvas').getContext){
        var rotatingRecordContainer = goog.dom.getElement('audio.rotatingRecord');
        var imageEl = goog.dom.getElement('record_pic');
        new kramerius.audio.ui.RotatingRecord(rotatingRecordContainer, controller, imageEl, loggingEnabled);    
    }else{
        var root = goog.dom.getElement('audio.html5Error')
        goog.dom.append(root,
            "Pro plnohodnotné přehrávání použijte některý z webových prohlížečů podporujících HTML 5 canvas. ");
        goog.dom.append(root, "Doporučujeme například ");
        goog.dom.append(root, goog.dom.createDom(
            'a',{
                href: 'http://www.firefox.com'
            }, "Firefox"));
        goog.dom.append(root, ', ');
        goog.dom.append(root, goog.dom.createDom(
            'a',{
                href: 'http://www.google.com/chrome'
            }, "Chrome"));
        goog.dom.append(root, ', ');
        goog.dom.append(root, goog.dom.createDom(
            'a',{
                href: 'http://www.opera.com'
            }, "Operu"));
        goog.dom.append(root, ', ');
        goog.dom.append(root, goog.dom.createDom(
            'a',{
                href: 'http://www.apple.com/safari'
            }, "Safari"));
        goog.dom.append(root, ', nebo Internet Explorer verze 9 a vyšší.');
    }
    
    this.log_('finished rendering');
    
//    //DEPRECATED
//    //stop button
//    this.log_('rendering STOP button');
//    new kramerius.audio.ui.StopButton(
//        goog.dom.getElement('audio.stopBtn'), controller);
//    //volume info
//    this.log_('rendering volume info');
//    new kramerius.audio.ui.VolumeInfo(
//        goog.dom.getElement('audio.volumeInfo'), controller);
//       
    
}

kramerius.audio.Renderer.prototype.initAudioPlayer_ = function(controller, swfUrl, loggingEnabled){
    controller.setAudioPlayer(
        new kramerius.audio.AudioPlayer(controller, loggingEnabled, swfUrl)
        );
}


/** @private */
kramerius.audio.Renderer.prototype.log_ = function(text){
    if (this.loggingEnabled_){
        console.log("Renderer: " + text);
    }
}

/** 
 * @expose 
 * @param {!string} pid Persistent identifier of the track object in Fedora repository.
 * @param {!string} title Name of the track.
 * @param {!string} length Length of the track
 * @param {!boolean} hasMp3 if mp3 datastream is available for this track
 * @param {!boolean} hasOgg if ogg datastream is available for this track
 * @param {!boolean} hasWav if wav datastream is available for this track
 **/
kramerius.audio.Renderer.prototype.addNewTrack = function(pid, title, length, hasMp3, hasOgg, hasWav){
    this.log_("adding track: id=" + pid + ", title=" + title);
    var track = this.trackFactory_.createTrack(pid,title,length, hasMp3, hasOgg, hasWav);
    var newItemSelected;
    if(this.playlist_.getSelectedItem() == undefined){
        newItemSelected = true;
        this.controller_.setTrack(track, false);
    }else{
        newItemSelected = false;    
    }
    var playlistItem = new kramerius.audio.ui.playlist.Item(track, newItemSelected);
    this.playlist_.addItem(playlistItem);    
}

/** 
 * @expose 
 *
 **/
kramerius.audio.Renderer.prototype.appendCss = function(url, id){
    this.log_("appending css from " + url);
    //TODO: podle id zkontrolovat, jestli uz neni nacteno
    var link = goog.dom.createDom(
        'link',
        {
            id: id,
            rel: 'stylesheet',
            type: 'text/css',
            href: url
        });
    goog.dom.appendChild(document.body, link);
}

